<!DOCTYPE html>
<html>

  <!--头文件-->
  {include file='head'}

<section class="rt_wrap content mCustomScrollbar">
 <div class="rt_content">
      <div class="page_title">
       <h2 class="fl">添加/更新会员等级</h2>
          <a href="level" class="fr top_rt_btn add_icon">添加等级</a>

      </div>

      <table class="table" name="site1" site="{$webSite}">
          {if $flag == 'lid'}
                {foreach $level as $lev0}
                    {if $lev0['level_id'] == $lid1}
           <tr>
            <td style="text-align:right;">会员等级：</td>
            <td>
             <input type="text" class="textbox" value="{$lev0['level_name']}" placeholder="等级名称" maxlength="15"  name="levName1" oldName="{$lev0['level_name']}"/>
             <div>&nbsp;</div>
             <div class="tips"></div>
            </td>
            <td style="text-align:right;">等级积分：</td>
            <td>
             <input type="text" class="textbox" value="{$lev0['level_grade']}" placeholder="等级积分" maxlength="10"  name="levGrade1" oldGrade="{$lev0['level_grade']}"/>
             <div>&nbsp;</div>
             <div class="tips"></div>
            </td>
            <td style="text-align:right;">享受折扣率：</td>
            <td>
             <input type="text" class="textbox" value="{$lev0['discount']}" placeholder="0-1的小数" maxlength="5" name="levCount1" oldCount="{$lev0['discount']}"/>
             <div>&nbsp;</div>
             <div class="tips"></div>
            </td>
            <td>
             <a class="full_link_td" style="line-height:58px" name="update" updater="{$lev0['level_id']}">修改</a>
            </td>
           </tr>
                    {/if}
                {/foreach}
          {else}

          <tr>
              <td style="text-align:right;">会员等级：</td>
              <td>
                  <input type="text" class="textbox" value="" placeholder="等级名称" maxlength="15"  name="levName"/>
                  <div>&nbsp;</div>
                  <div class="tips"></div>
              </td>
              <td style="text-align:right;">等级积分：</td>
              <td>
                  <input type="text" class="textbox" value="" placeholder="等级积分" maxlength="10"  name="levGrade"/>
                  <div>&nbsp;</div>
                  <div class="tips"></div>
              </td>
              <td style="text-align:right;">享受折扣率：</td>
              <td>
                  <input type="text" class="textbox" value="" placeholder="0-1的小数" maxlength="5" max="1"  name="levCount"/>
                  <div>&nbsp;</div>
                  <div class="tips"></div>
              </td>
              <td>
                  <a class="full_link_td" style="line-height:58px" name="btn">确认</a>
              </td>
          </tr>
          {/if}
      </table>
      <div class="page_title">
       <h2 class="fl">等级列表</h2>
      </div>
      <table class="table">
       <tr>
        <th>Id</th>
        <th>会员等级</th>
        <th>等级积分</th>
        <th>享受折扣率</th>
        <th>操作</th>
       </tr>

       {foreach $level as $lev}
       <tr>
        <td class="center" name="">{$lev['level_id']}</td>
        <td class="center" name="">{$lev['level_name']}</td>
        <td class="center" name="">{$lev['level_grade']}</td>
        <td class="center" name="">{$lev['discount']}</td>
        <td class="center">

            <!--ajax-->
         <!--<a title="编辑" class="link_icon" name="edit" editor="{$lev['level_id']}">&#101;</a>-->
            <!--url链接-->
            <a title="编辑" href="level?lid1={$lev['level_id']}" class="link_icon">&#101;</a>
            <!--ajax-->
         <a title="删除" class="link_icon" name="del" deleter="{$lev['level_id']}">&#100;</a>
            <!--url链接-->
         <!--<a title="删除" href="delete?lid2={$lev['level_id']}" class="link_icon">&#100;</a>-->
        </td>
       </tr>
       {/foreach}

      </table>
      <aside class="paging">
      </aside>
 </div>
</section>
</body>
</html>
<script>
     //添加
     $(function () {

         var fName = false;
         var fGrade = false;
         var fCount = false;
         var $levName = null;
         var $levGrade = null;
         var $levCount = null;

         //等级名称
         $('[name=levName]').blur(function () {

             //取值
              $levName = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

                 //利用正则判断汉字
                 if($levName.match( /^[\u4E00-\u9FA5]{1,}$/)){
                     //alert('这是汉字');
                     if ($levName.length  > 6 || $levName.length < 0) {
                         $('.tips').eq(0).html('应为 1~6 个汉字').css('color','red');
                         fName = false;
                         return false;
                     }

                 } else {
                     //alert('这不是汉字');
                     if ($levName.length > 15 || $levName.length < 1) {
                         $('.tips').eq(0).html('应为 1~15 位字符串').css('color','red');
                         fName = false;
                         return false;
                     }
                 }

             fName = true;
             $('.tips').eq(0).html('合法').css('color','green');
         });

         //等级积分
         $('[name=levGrade]').blur(function () {

             //关联
             if (!fName) {

                 $('.tips').eq(1).html('请先完成上一步').css('color','red');
                 return false;
             }
            //取值
             $levGrade = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

             //是否为空
             if ($levGrade.length == 0) {

                 $('.tips').eq(1).html('不得为空').css('color','red');
                 fGrade = false;
                 return false;
             }

             //是否为纯数字,isNaN() 是数字-->返回false,否则返回true
             if(isNaN($levGrade)){

                 $('.tips').eq(1).html('只能为大于0的纯数字').css('color','red');
                 fGrade = false;
                 return false;
             }


             //console.log(typeof($levGrade)); //字符串
             if (parseInt($levGrade) <= 0) {

                 $('.tips').eq(1).html('只能为大于0的纯数字').css('color','red');
                 fGrade = false;
                 return false;
                 alert(43546784654);
             }


             fGrade = true;
             $('.tips').eq(1).html('合法').css('color','green');

         });

         //折扣率
         $('[name=levCount]').blur(function () {

             //关联
             if (!fGrade) {

                 $('.tips').eq(2).html('请先完成上一步').css('color','red');
                 return false;
             }

             //取值
             $levCount = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

             if ($levCount.length == 0) {

                 $('.tips').eq(2).html('不得为空').css('color','red');
                 fCount = false;
                 return false;
             }
             //是否为纯数字,isNaN() 是数字-->返回false,否则返回true
             if(isNaN($levCount)){

                 $('.tips').eq(2).html('必须为纯数字').css('color','red');
                 fCount = false;
                 return false;
             }

             if($levCount <= 0 || $levCount > 1){

                 $('.tips').eq(2).html('必须是0-1的纯数字').css('color','red');
                 fCount = false;
                 return false;
             }
             //根据小数点切割字符串
             //var $sub = $levCount.split('.')[1];

             fCount = true;
             $('.tips').eq(2).html('合法').css('color','green');
         });

         //点击事件
         $('[name=btn]').click(function () {

             if (fName && fGrade && fCount && $levName && $levGrade && $levCount) {

                 //ajax
                 $.post('add',{levName:$levName,levGrade:$levGrade,levCount:$levCount},success,'json');

             } else {
                 alert('请重新填写所有输入框');
             }
         });
         $levName = null;
         $levGrade = null;
         $levCount = null;

         function success(data)
         {
             //console.log(data);

             switch (data.state) {
                 //添加成功
                 case 1:
                     alert(data.tip);
                     fName = false;
                     fGrade = false;
                     fCount = false;
                     break;

                //添加失败
                 case 2:
                     alert(data.tip);
                     fName = false;
                     fGrade = false;
                     fCount = false;
                     break;
             }
             //返回上一个页面并刷新
             //location.replace(document.referrer);
             location.replace('level'); //返回并刷新添加页面
         }
     });

     //编辑
     $(function () {

         //标记位
         var fName1 = true;
         var fGrade1 = true;
         var fCount1 = true;

         //原数据
         var $oldName  = $('[name=levName1]').attr('oldName');
         var $oldGrade = $('[name=levGrade1]').attr('oldGrade');
         var $oldCount = $('[name=levCount1]').attr('oldCount');

         //变量值
         var $oName = null;
         var $oGrade = null;
         var $oCount = null;
         var arr = [];  //存放值
         var fArr = []; //标记

         var $oSite = $('[name=site1]').attr('site'); //站点绝对
         //console.log();

         //等级名称
         $('[name=levName1]').blur(function () {

             //取值
             $oName = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

             //比对等级名称
             if ($oldName == $oName) {

                 $('.tips').eq(0).html('等级名称未修改').css('color','blue');
                 fName1 = true;

             } else {

                 arr['level_name'] = null;
                 fArr = [];

                 //利用正则判断汉字
                 if($oName.match( /^[\u4E00-\u9FA5]{1,}$/)){
                     //alert('这是汉字');
                     if ($oName.length  > 6 || $oName.length < 0) {
                         $('.tips').eq(0).html('应为 1~6 个汉字').css('color','red');
                         fName1 = false;
                         return false;
                     }

                 } else {
                     //alert('这不是汉字');
                     if ($oName.length > 15 || $oName.length < 1) {
                         $('.tips').eq(0).html('应为 1~15 位字符串').css('color','red');
                         fName1 = false;
                         return false;
                     }
                 }

                 fName1 = true;
                 arr['level_name'] = $oName;
                 fArr.push($oName);
                 $('.tips').eq(0).html('合法').css('color','green');
             }
         });

         //等级积分
         $('[name=levGrade1]').blur(function () {

             //关联
             if (!fName1) {

                 $('.tips').eq(1).html('请先完成上一步').css('color','red');
                 return false;
             }
             //取值
             $oGrade = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

             //比对等级分数
             if ($oldGrade == $oGrade) {

                 $('.tips').eq(1).html('等级分数未修改').css('color','blue');
                 fGrade1 = true;

             } else {

                 //清空
                 arr['level_grade'] = null;
                 fArr = [];

                 //是否为空
                 if ($oGrade.length == 0) {

                     $('.tips').eq(1).html('不得为空').css('color','red');
                     fGrade1 = false;
                     return false;
                 }

                 //是否为纯数字,isNaN() 是数字-->返回false,否则返回true
                 if(isNaN($oGrade)){

                     $('.tips').eq(1).html('只能为大于0的纯数字').css('color','red');
                     fGrade1 = false;
                     return false;
                 }


                 //console.log(typeof($levGrade)); //字符串
                 if (parseInt($oGrade) <= 0) {

                     $('.tips').eq(1).html('只能为大于0的纯数字').css('color','red');
                     fGrade1 = false;
                     return false;
                 }


                 fGrade1 = true;
                 arr['level_grade'] = $oGrade;
                 fArr.push($oGrade);
                 $('.tips').eq(1).html('合法').css('color','green');

             }
         });

         //折扣率
         $('[name=levCount1]').blur(function () {

             //关联
             if (!fGrade1) {

                 $('.tips').eq(2).html('请先完成上一步').css('color','red');
                 return false;
             }

             //取值
             $oCount = $(this).val().replace(/(^\s*)|(\s*$)/g, "");

             //比对等级分数
             if ($oldCount == $oCount) {

                 $('.tips').eq(2).html('折扣率未修改').css('color','blue');
                 fCount1 = true;

             } else {

                 //清空
                 arr['discount'] = null;
                 fArr = [];

                 //是否为空
                 if ($oCount.length == 0) {

                     $('.tips').eq(2).html('不得为空').css('color','red');
                     fCount1 = false;
                     return false;
                 }
                 //是否为纯数字,isNaN() 是数字-->返回false,否则返回true
                 if(isNaN($oCount)){

                     $('.tips').eq(2).html('必须为纯数字').css('color','red');
                     fCount1 = false;
                     return false;
                 }

                 if($oCount <= 0 || $oCount > 1){

                     $('.tips').eq(2).html('必须是0-1的纯数字').css('color','red');
                     fCount1 = false;
                     return false;
                 }
                 //根据小数点切割字符串
                 //var $sub = $levCount.split('.')[1];

                 fCount1 = true;
                 arr['discount'] = $oCount;
                 fArr.push($oCount);
                 $('.tips').eq(2).html('合法').css('color','green');
             }

         });

         //修改提交
         $('[name=update]').click(function () {

             //id值
             var $lid1 = $(this).attr('updater');

             //console.log(fArr.length);
             if (fName1 && fGrade1  && fCount1) {

                 if (fArr.length != 0) {

                     var params = [];
                     for (var key in arr) {

                         var $k = key;      //键
                         var $v = arr[key]; //值

                         //压入堆栈
                         if ($v != null) {
                             params.push($k +'==='+ $v);
                         }
                     }
                     var $str = params.join();
                     //console.log($str);
                     //console.log($lid1);

                     //ajax
                     $.post('update',{lid1:$lid1,str:$str},success1,'json');

                 } else {
                     alert('你未修改数据');
                 }
             }

         });

         //回调函数
         function success1(data)
         {
             console.log(data);

             switch (data.status) {
                 case 1:
                     alert(data.tip);
                     break;
                 case 2:
                     alert(data.tip);
                     break;
             }
             //刷新当前页面
             //window.location.reload();
             location.replace('level'); //返回并刷新添加页面
         }
     });

    //删除
    $(function () {

        //点击事件
        $('[name=del]').click(function () {

            //id值
            var $lid2 = $(this).attr('deleter');

            //ajax
            $.post('delete',{lid2:$lid2},success2,'json');
        });

        //回调函数
        function success2(data)
        {
            //console.log(data);

            switch (data.state2) {
                case 1:
                    alert(data.tip2);
                    break;
                case 2:
                    alert(data.tip2);
                    break;
                case 3:
                    alert(data.tip2);
                    break;
            }
            //window.location.reload();
            location.replace('level'); //返回并刷新添加页面
        }
    });

</script>
